package com.zhuoyue.web.controller.v2;

import com.zhuoyue.biz.core.api.domain.AppUserDO;
import com.zhuoyue.biz.core.api.dto.rep.AppUserRepDTO;
import com.zhuoyue.biz.core.api.dto.req.UserReqDTO;
import com.zhuoyue.biz.core.api.dto.req.QuickLoginReqDTO;
import com.zhuoyue.biz.core.api.service.impl.AppUserService;

import com.zhuoyue.common.aop.auth.CheckLogin;
import com.zhuoyue.common.aop.auth.User;
import com.zhuoyue.common.aop.version.ApiVersion;
import com.zhuoyue.common.dto.ControllerResult;
import com.zhuoyue.common.group.ValidationGroups;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wjming
 * @ClassName: AppUserController
 * @Description: 用户中心 用户操作接口
 * @Date 2021/4/17 12:30
 */
@RestController
@Validated
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppUserV2Controller {

    private final AppUserService appUserService;

    /**
     * 微信快速登陆
     */
    private static final String TYPE_WX = "0";
    /**
     * QQ快速登陆
     */
    private static final String TYPE_QQ = "1";
    /**
     * 苹果快速登陆
     */
    private static final String TYPE_APPLE = "2";

    /**
     * @Description: APP用户注册
     * @param: userReqDTO
     * @param: organizationCode
     * @return: com.zhuoyue.common.dto.ControllerResult
     * @author:wjming
     * @Date:2021-06-28 18:09
     */
//    @PostMapping("/api/user/register")
//    @ApiVersion(value = "1.4.0")
    public ControllerResult register(@Validated({ValidationGroups.Insert.class}) @RequestBody UserReqDTO userReqDTO, @RequestHeader String organizationCode) throws Exception {

        AppUserDO requestUserDO = new AppUserDO().registerDtoToAppUserDo(userReqDTO, organizationCode);

        //只有openID不为空，快速登陆的ID也不为空的时候才能进行openID的赋值，否则不予以赋值
        if (StringUtils.isNotEmpty(userReqDTO.getOpenId()) && StringUtils.isNotEmpty(userReqDTO.getType())) {
            requestUserDO = setOpenId(requestUserDO, userReqDTO.getOpenId(), userReqDTO.getType());
        } else {
            requestUserDO.setType(null);
        }

        AppUserRepDTO register = appUserService.register(requestUserDO);

        return ControllerResult.toReturnDataSuccess("用户注册成功!", register);
    }


    /**
     * 用户账号登陆
     * 已测试
     * 登陆方式多样：账号密码，苹果,QQ,微信快速登陆
     *
     * @param organizationCode 组织编码
     * @param userReqDTO       登陆对象信息
     * @return
     */
//    @PostMapping("/api/user/login")
//    @ApiVersion(value = "1.4.0")
    public ControllerResult<AppUserDO> login(@Validated({ValidationGroups.Insert.class}) @RequestBody UserReqDTO userReqDTO, @RequestHeader String organizationCode) {

        AppUserDO requestUserDO = new AppUserDO().loginDtoToAppUserDo(userReqDTO, organizationCode);

        //只有openID不为空，快速登陆的ID也不为空的时候才能进行openID的赋值，否则不予以赋值
        if (StringUtils.isNotEmpty(userReqDTO.getOpenId()) && StringUtils.isNotEmpty(userReqDTO.getType())) {
            requestUserDO = setOpenId(requestUserDO, userReqDTO.getOpenId(), userReqDTO.getType());
        } else {
            requestUserDO.setType(null);
        }

        //通过账号密码登陆
        AppUserRepDTO appUserRepDTO = appUserService.loginByAccountAndPsd(requestUserDO);

        return ControllerResult.toReturnDataSuccess("登录成功！", appUserRepDTO);
    }

    /**
     * @Description: 用户快速登陆，如微信、QQ、苹果等
     * @param: quickLoginReqDTO
     * @param: organizationCode
     * @return: com.zhuoyue.common.dto.ControllerResult
     * @author: wjming
     * @Date: 2021-06-28 11:52
     */
    @PostMapping("/api/user/quick/login")
    public ControllerResult quickLogin(@Validated @RequestBody QuickLoginReqDTO quickLoginReqDTO, @RequestHeader String organizationCode) {
        AppUserDO appUserDO = new AppUserDO().quickLoginDtoToAppUserDo(quickLoginReqDTO, organizationCode);

        appUserDO = setOpenId(appUserDO, quickLoginReqDTO.getOpenId(), quickLoginReqDTO.getType());
        AppUserRepDTO appUserRepDTO = appUserService.quickLogin(appUserDO);

        return ControllerResult.toReturnDataSuccess("登陆成功", appUserRepDTO);
    }


    /**
     * 修改用户信息
     *
     * @param request
     * @param userReqDTO
     * @return
     */
//    @PutMapping("/api/user/modify")
//    @CheckLogin
//    @ApiVersion(value = "1.4.0")
    public ControllerResult modifyAppUserInfo(HttpServletRequest request, @Validated({ValidationGroups.Update.class})@RequestBody UserReqDTO userReqDTO) {
        User user = (User) request.getAttribute("user");

        AppUserDO appUser = new AppUserDO().modifyUserBaseMessage(userReqDTO, user.getId());
        AppUserRepDTO appUserRepDTO = appUserService.updateUserMessage(appUser);
        return ControllerResult.toReturnDataSuccess("用户信息修改成功!", appUserRepDTO);
    }


    /**
     * 赋值快速登陆的openId
     *
     * @param appUserDO
     * @param openId
     * @param type
     * @return
     */
    private AppUserDO setOpenId(AppUserDO appUserDO, String openId, String type) {
        appUserDO.setOpenidWeiXin(TYPE_WX.equals(type) ? openId : null);
        appUserDO.setOpenidQQ(TYPE_QQ.equals(type) ? openId : null);
        appUserDO.setOpenidApple(TYPE_APPLE.equals(type) ? openId : null);
        return appUserDO;
    }
}
